<?php

namespace App\Http\Controllers\CmsApi;

use Illuminate\Http\Request;
use App\Http\Model\Hint;
use App\Lib\Response;
use App\Http\Controllers\BaseController;
use App\Http\Services\RoomServices;
use App\Http\Services\NewRoomServices;

class InnerRoomController extends BaseController
{
    protected $room_service;
    protected $newRoomService;

    public function __construct()
    {
        $this->room_service = new RoomServices();
        $this->newRoomService = new NewRoomServices();
    }

    //房间收藏
    public function post_collect(Request $request){
        $user_id = $request->get("user_id");
        $follow_id = $request->get("follow_id");
        if(empty($user_id) || empty($follow_id)){
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = null;
        if (!$request->get("action")) {
            $data = $this->room_service->add_collect($user_id, $follow_id);
        } else {
            $data = $this->room_service->del_collect($user_id, $follow_id);
        }

        Response::returnResult(Hint::SUCCESS);
    }
    /**
     * 新增房间活动特殊封面配置
     * @param Request $request
     */
    public function SpecialRoomCreateOrUp(Request $request)
    {
        $roomid = $request->post('roomid');
        $imgurl = $request->post('imgurl');
        $start_time = $request->post('start_time');
        $end_time = $request->post('end_time');
        if (empty($roomid))
        {
            Response::returnResult(Hint::PARAM_ERROR, [],'房间id不能为空');
        }
        if (empty($imgurl))
        {
            Response::returnResult(Hint::PARAM_ERROR,[],'图片地址不能为空');
        }
        if (empty($start_time))
        {
            Response::returnResult(Hint::PARAM_ERROR, [],'开始时间不能为空');
        }
        if (empty($end_time))
        {
            Response::returnResult(Hint::PARAM_ERROR, [],'结束时间不能为空');
        }
        //数据准备入库
        $status = $this->room_service->specialRoomCreateOrUp($roomid,$imgurl,$start_time,$end_time);
        Response::returnResult(Hint::SUCCESS,[],$status);
    }

    /**
     * 删除活动房间配置
     */
    public function SpecialRoomDel(Request $request)
    {
        $roomid = $request->post('roomid');
        if (empty($roomid))
        {
            Response::returnResult(Hint::PARAM_ERROR, [],'房间id不能为空');
        }
        $status = $this->room_service->specialRoomDel($roomid);
        Response::returnResult(Hint::SUCCESS,[],$status);
    }

    /**
     * 新增置顶房间
     */
    public function topRoomCreate(Request $request)
    {
        $roomId = (int) $request->input('room_id');
        $sortNum = (int) $request->input('sort_num');
        $timeSt = (int) $request->input('time_st');
        $timeEd = (int) $request->input('time_ed');
        if(empty($roomId) || empty($timeSt) || empty($timeEd)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->newRoomService->topRoomCreate($roomId, $sortNum, $timeSt, $timeEd);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 修改置顶房间
     */
    public function topRoomUpdate(Request $request)
    {
        $roomId = (int) $request->input('room_id');
        $sortNum = (int) $request->input('sort_num');
        $timeSt = (int) $request->input('time_st');
        $timeEd = (int) $request->input('time_ed');
        if(empty($roomId) || empty($timeSt) || empty($timeEd)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->newRoomService->topRoomUpdate($roomId, $sortNum, $timeSt, $timeEd);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 删除置顶房间
     */
    public function topRoomDel(Request $request)
    {
        $roomId = (int) $request->input('room_id');
        if(empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->newRoomService->topRoomDel($roomId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 获取开启的房间
     */
    public function getOpenRoomIds(Request $request)
    {
        $roomIds = $request->input('roomIds');
        $data = $this->newRoomService->getOpenRoomIds($roomIds);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 设置房间分成用户
     */
    public function setRoomFinanceUser(Request $request)
    {
        $userId = (int) $request->input('user_id', 0);
        $roomId = (int) $request->input('room_id', 0);
        $adminId = (int) $request->input('admin_id',0);
        if(empty($roomId) || empty($adminId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $key = 'ROOM:setRoomFinanceUser:'.$roomId;
        if (!redis_lock($key, 30)){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->newRoomService->setRoomFinanceUser($userId, $roomId, $adminId);
        redis_unlock($key);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 审核房间欢迎语
     */
    public function auditRoomWelcomeWord(Request $request)
    {
        $id = (int) $request->input('id');
        $auditStatus = (int) $request->input('audit_status');
        if(empty($id) || empty($auditStatus)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->newRoomService->auditRoomWelcomeWord($id, $auditStatus);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 一键通过房间欢迎语
     */
    public function auditBatchRoomWelcomeWord(Request $request)
    {
        $ids = (string) $request->input('ids');
        $ids = explode(',', $ids);
        if(empty($ids)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        foreach($ids as $id) {
            $data = $this->newRoomService->auditRoomWelcomeWord($id, 2);
        }

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    //隐藏房间
    public function hideRoom(Request $request){
        $roomId = $request->post('room_id',0);
        $hideStatus = $request->post('status',0);
        $res = $this->newRoomService->hideRoom($roomId, $hideStatus);
        Response::returnResult($res['code'], $res['data'], $res['msg']);
    }
}